package com.vincce.sport.core.utils;

import com.vincce.sport.entity.*;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xupeng
 * Date: 13-12-4
 * Time: 下午10:04
 * To change this template use File | Settings | File Templates.
 */
public class ConvertUtil {
    public static String encryptMD5(String str) throws Exception {
        byte[] data=str.getBytes();
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(data);
        byte b[] = md5.digest();
        int x;
        StringBuffer buf = new StringBuffer("");
        for (int i = 0; i < b.length; i++) {
            x = b[i];
            if (x < 0)
                x += 256;
            if (x < 16)
                buf.append("0");
            buf.append(Integer.toHexString(x));
        }
        System.out.println("32位加密后的字符串: " + buf.toString());// 32位的加密
        return  buf.toString();
    }
    public static String format(String formatStr,Date date)
    {
        if(CheckUtil.isEmpty(date))
        {
            return "";
        }

        return new SimpleDateFormat(formatStr).format(date);
    }
    
    public static float percentNum(float num1, float num2){
    	if(num2 == 0){
    		return 0;
    	}
    	BigDecimal bigDecimal1 = new BigDecimal(num1);
    	BigDecimal bigDecimal2 = new BigDecimal(num2);
    	BigDecimal result = bigDecimal1.divide(bigDecimal2,4,BigDecimal.ROUND_HALF_DOWN);
    	return result.floatValue();
    }

    /**
     *
     * @param school 学校数据
     * @param teamDate1 主队数据
     * @param teamDate2 副队数据
     * @param playerDates1 主队球员
     * @return
     */
    public static TeamCreateImg createByDate(School school1,School school2,TeamDate teamDate1,TeamDate teamDate2,List<PlayerDate> playerDates1){
        TeamCreateImg teamCreateImg=new TeamCreateImg();
        teamCreateImg.setList(createByPlayDate(playerDates1));
        teamCreateImg.setTeamId(teamDate1.getTeamId());
        teamCreateImg.setTwoPointsTotal(teamDate1.getTwoPointsTotal());
        teamCreateImg.setTwoPointsHitTotal(teamDate1.getTwoPointsHitTotal());
        teamCreateImg.setThreePointsTotal(teamDate1.getThreePointsTotal());
        teamCreateImg.setThreePointsHitTotal(teamDate1.getThreePointsHitTotal());
        teamCreateImg.setFreeThrowTotal(teamDate1.getFreeThrowTotal());
        teamCreateImg.setFreeThrowHitTotal(teamDate1.getFreeThrowHitTotal());
        teamCreateImg.setFreeThrowHitRate(teamDate1.getFreeThrowHitRate());
        teamCreateImg.setThreePointsHitRate(teamDate1.getThreePointsHitRate());
        teamCreateImg.setTwoPointsHitRate(teamDate1.getTwoPointsHitRate());
        teamCreateImg.setFrontBackboard(teamDate1.getFrontBackboard());
        teamCreateImg.setBackboard(teamDate1.getBackboard());
        teamCreateImg.setSteals(teamDate1.getSteals());
        teamCreateImg.setCapping(teamDate1.getCapping());
        teamCreateImg.setFault(teamDate1.getFault());
        teamCreateImg.setFoul(teamDate1.getFoul());
        teamCreateImg.setAssistsAsts(teamDate1.getAssistsAsts());
        teamCreateImg.setTotalScore(teamDate1.getTotalScore());
        teamCreateImg.setTotalHit(teamDate1.getTotalHit());
        teamCreateImg.setTeamName(teamDate1.getTeamName());
        teamCreateImg.setLogUrl(school1.getLogoUrl());
        teamCreateImg.setFirstSectionScore(teamDate1.getFirstSectionScore());
        teamCreateImg.setSecondSectionScore(teamDate1.getSecondSectionScore());
        teamCreateImg.setThirdSectionScore(teamDate1.getThirdSectionScore());
        teamCreateImg.setFourthSectionScore(teamDate1.getFourthSectionScore());
        //teamCreateImg.setFullTimeUrl();
        teamCreateImg.setTotalLossScore(teamDate1.getTotalLossScore());
        teamCreateImg.setShootBasketTotal(teamDate1.getShootBasketTotal());
        teamCreateImg.setShootBasketHitTotal(teamDate1.getShootBasketHitTotal());
        teamCreateImg.setSchoolName(school1.getName());
        //--------对手数据
        teamCreateImg.setOpponentName(teamDate2.getTeamName());
        teamCreateImg.setOpponentTeamId(teamDate2.getTeamId());
        teamCreateImg.setOpponentTwoPointsTotal(teamDate2.getTwoPointsTotal());
        teamCreateImg.setOpponentTwoPointsHitTotal(teamDate2.getTwoPointsHitTotal());
        teamCreateImg.setOpponentThreePointsTotal(teamDate2.getThreePointsTotal());
        teamCreateImg.setOpponentThreePointsHitTotal(teamDate2.getThreePointsHitTotal());
        teamCreateImg.setOpponentFreeThrowTotal(teamDate2.getFreeThrowTotal());
        teamCreateImg.setOpponentFreeThrowHitTotal(teamDate2.getFreeThrowHitTotal());
        teamCreateImg.setOpponentFreeThrowHitRate(teamDate2.getFreeThrowHitRate());
        teamCreateImg.setOpponentThreePointsHitRate(teamDate2.getThreePointsHitRate());
        teamCreateImg.setOpponentTwoPointsHitRate(teamDate2.getTwoPointsHitRate());
        teamCreateImg.setOpponentFrontBackboard(teamDate2.getFrontBackboard());
        teamCreateImg.setOpponentBackBackboard(teamDate2.getBackBackboard());
        teamCreateImg.setOpponentBackboard(teamDate2.getBackboard());
        teamCreateImg.setOpponentSteals(teamDate2.getSteals());
        teamCreateImg.setOpponentCapping(teamDate2.getCapping());
        teamCreateImg.setOpponentFault(teamDate2.getFault());
        teamCreateImg.setOpponentFoul(teamDate2.getFoul());
        teamCreateImg.setOpponentAssistsAsts(teamDate2.getAssistsAsts());
        teamCreateImg.setOpponentTotalScore(teamDate2.getTotalScore());
        teamCreateImg.setOpponentTotalHit(teamDate2.getTotalHit());
        teamCreateImg.setOpponentFirstSectionScore(teamDate2.getFirstSectionScore());
        teamCreateImg.setOpponentSecondSectionScore(teamDate2.getSecondSectionScore());
        teamCreateImg.setOpponentThirdSectionScore(teamDate2.getThirdSectionScore());
        teamCreateImg.setOpponentFourthSectionScore(teamDate2.getFourthSectionScore());
        //teamCreateImg.setOpponentFullTimeUrl();
        teamCreateImg.setOpponentLogUrl(school2.getLogoUrl());
        teamCreateImg.setOpponentShootBasketTotal(teamDate2.getShootBasketTotal());
        teamCreateImg.setOpponentShootBasketHitTotal(teamDate2.getShootBasketHitTotal());
        teamCreateImg.setOpponentSchoolName(school2.getName());
        return teamCreateImg;
    }

    public static List<PlayerCreateImg> createByPlayDate(List<PlayerDate> playerDate){
        List<PlayerCreateImg> playerCreateImgList=new ArrayList<PlayerCreateImg>();
        PlayerCreateImg playerCreateImg1=null;
        for(int i=0;i<playerDate.size();i++){
            playerCreateImg1=new PlayerCreateImg();
            playerCreateImg1.setThreePointsTotal(playerDate.get(i).getThreePointsTotal());
            playerCreateImg1.setThreePointsHitTotal(playerDate.get(i).getThreePointsHitTotal());
            playerCreateImg1.setTwoPointsTotal(playerDate.get(i).getTwoPointsTotal());
            playerCreateImg1.setTwoPointsHitTotal(playerDate.get(i).getTwoPointsHitTotal());
            playerCreateImg1.setFreeThrowTotal(playerDate.get(i).getFreeThrowTotal());
            playerCreateImg1.setFreeThrowHitTotal(playerDate.get(i).getFreeThrowHitTotal());
            playerCreateImg1.setFreeThrowHitRate(playerDate.get(i).getFreeThrowHitRate());
            playerCreateImg1.setThreePointsHitRate(playerDate.get(i).getThreePointsHitRate());
            playerCreateImg1.setTwoPointsHitRate(playerDate.get(i).getTwoPointsHitRate());
            playerCreateImg1.setFrontBackboard(playerDate.get(i).getFrontBackboard());
            playerCreateImg1.setBackBackboard(playerDate.get(i).getBackBackboard());
            playerCreateImg1.setBackboard(playerDate.get(i).getBackboard());
            playerCreateImg1.setSteals(playerDate.get(i).getSteals());
            playerCreateImg1.setCapping(playerDate.get(i).getCapping());
            playerCreateImg1.setFault(playerDate.get(i).getFault());
            playerCreateImg1.setFoul(playerDate.get(i).getFoul());
            playerCreateImg1.setAssistsAsts(playerDate.get(i).getAssistsAsts());
            playerCreateImg1.setTotalRate(playerDate.get(i).getTotalRate());
            playerCreateImg1.setTotalScore(playerDate.get(i).getTotalScore());
            playerCreateImg1.setPlayingTime(playerDate.get(i).getPlayingTime());
            playerCreateImg1.setPlayerName(playerDate.get(i).getName());
            playerCreateImg1.setPlayerNo(playerDate.get(i).getPlayerNo());
            playerCreateImgList.add(playerCreateImg1);
        }
        return playerCreateImgList;
    }
    public static void main(String[] args) {
		System.out.println(percentNum(12, 34));
	}
}
